import { INVALID_VALUE } from "../../../../Core/CoreDefine";
import { MapWrap, ObjectWrap } from "../../../../Core/FrameEx/ES5Ex";
import GameMgr from "../../../Logic/GameMgr";
import { ITEM_TYPE, PART_EQUIP, PART_PROPTS } from './../../../Common/Define';
import RJXItem from "./RJXItem";


export class REquireItem extends ObjectWrap {
	public insId: string;
	public equipId: number;
	/**等级 */
	public sLv: number
	/**品質 */
	public quality: number;
	/**是对应的item 实例 */
	public itemData: RJXItem;
	/**是否首次增加穿戴经验 */
	public isFirstExp: boolean = false;
	public wear: number = INVALID_VALUE;
	public propts: MapWrap<number, number> = new MapWrap<number, number>();
	constructor(equip: any[], itemData?: RJXItem) {
		super();
		this.updateData(equip, itemData);
	}

	/**更新数据 */
	public updateData(equip: any[], itemData?: RJXItem) {
		this.insId = equip[PART_EQUIP.INS_ID];
		this.equipId = equip[PART_EQUIP.TPLT_ID];
		this.sLv = equip[PART_EQUIP.SLV];
		this.wear = equip[PART_EQUIP.WEAR] ? equip[PART_EQUIP.WEAR] : -1;
		if (equip[PART_EQUIP.ISFIRSTEXP] > -2 || equip[PART_EQUIP.ISFIRSTEXP] != true) {
			this.isFirstExp = false;
		} else {
			this.isFirstExp = equip[PART_EQUIP.ISFIRSTEXP];
		}

		let raw = GameMgr.equipData.getRaw<SEquipDataRaw>(this.equipId);
		if (!raw) return;
		this.quality = raw.trait;
		if (!itemData) {
			this.itemData = new RJXItem([this.equipId, ITEM_TYPE.EQUIPDATA, 1, this]);
		}
		else {
			this.itemData = itemData;
		}
		this.updateProp();
	}

	/**geng*/
	public updateProp() {
		this.propts.clear();
		for (let i = 0; i < this.itemData.raw.property.length; i++) {
			let raw = this.itemData.raw;
			// if (!this.itemData.raw.property[i]) continue;
			this.propts.set(i, raw.property[i] + GameMgr.itemMgr.getEquipLvUpGrowArgs(raw.growPropty[i], this.sLv));
		}
	}

	/**显示下一级强化等级属性 */
	public nextUpdateProp(): MapWrap<number, number> {
		let propts: MapWrap<number, number> = new MapWrap<number, number>();
		for (let i = 0; i < this.itemData.raw.property.length; i++) {
			let raw = this.itemData.raw;
			let sLv = this.sLv + 1;
			propts.set(i, raw.property[i] + GameMgr.itemMgr.getEquipLvUpGrowArgs(raw.growPropty[i], sLv));
		}
		return propts
	}

	/**显示上一级强化等级属性 */
	public topUpdateProp(): MapWrap<number, number> {
		let propts: MapWrap<number, number> = new MapWrap<number, number>();
		for (let i = 0; i < this.itemData.raw.property.length; i++) {
			let raw = this.itemData.raw;
			let sLv = this.sLv - 1;
			propts.set(i, raw.property[i] + GameMgr.itemMgr.getEquipLvUpGrowArgs(raw.growPropty[i], sLv));
		}
		return propts
	}

	public getZhanli() {
		let propts = this.propts.values<number>()
		let zhanli = ((propts[PART_PROPTS.GongJi] + propts[PART_PROPTS.FangYu] + propts[PART_PROPTS.BingGong]
			+ propts[PART_PROPTS.BingKang] + propts[PART_PROPTS.HuoGong] + propts[PART_PROPTS.HuoKang]) * 3
			+ propts[PART_PROPTS.XueLiang]) * (1 + propts[PART_PROPTS.BaoJi] + propts[PART_PROPTS.ShanBi] + propts[PART_PROPTS.BaoShang])
		return zhanli;
	}
}